package com.loren.demo07.sockjs.config;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

@Slf4j
@Configuration
@EnableScheduling
@AllArgsConstructor
public class ScheduleConfig {

    private final SimpMessagingTemplate simpMessagingTemplate;

    @Scheduled(fixedRate = 20000L)
    public void groupTask() {
        String message = "send to group, timestamp = " + System.currentTimeMillis();
        log.info("group message = {}", message);
        // 将消息发送到通道：/group，具体为/group/message
        this.simpMessagingTemplate.convertAndSend("/group/message", message);
    }

    @Scheduled(fixedRate = 15000L)
    public void userTask() {
        String userId = "123";
        String message = "send to user, userId = " + userId + ", timestamp = " + System.currentTimeMillis();
        log.info("user message = {}", message);
        // 将消息发送到通道：/user，具体为/user/123/message
        this.simpMessagingTemplate.convertAndSendToUser("123", "/message", message);
    }
}
